﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace WebBanThuoc.DAO
{
    public class DataProvider : WebBanThuoc.DAO.IDataProvider
    {
        #region values
        private SqlConnection con;
        private SqlCommand com;
        private SqlDataAdapter dAdap;
        private SqlDataReader daReader;
        private String stringCon;        
        private bool hasData;
        private List<SqlParameter> dbParameters = null;

        #endregion

        #region Properties
        public bool HasData
        {
            get { return hasData; }
        }
        public SqlConnection Con
        {
            get { return con; }
        }
        public SqlCommand Com
        {
            get { return com; }
        }
        public SqlDataAdapter DAdap
        {
            get { return dAdap; }
        }
        public SqlDataReader DaReader
        {
            get { return daReader; }
        }

        public List<SqlParameter> DbParameters
        {
            get { return dbParameters; }
            set { dbParameters = value; }
        }
        #endregion

        private DataProvider()
        {

        }

        public DataProvider(SqlConnection connection)
        {
            this.con = connection;
            this.stringCon = connection.ConnectionString;
        }

        #region methods
        private void createNewConnection()
        {
            this.con = new SqlConnection(stringCon);
        }
        public void openConnection()
        {
            try
            {
                if (this.con != null)
                {
                    if (this.con.State == ConnectionState.Open)
                    {
                        con.Close();
                    }
                }
                else
                {
                    this.con = new SqlConnection(stringCon);
                }
                this.con.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void openNewConnection()
        {
            try
            {
                this.con = new SqlConnection(stringCon);
                this.con.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void closeConnection()
        {
            try
            {
                if (this.con != null)
                {
                    if (this.con.State != ConnectionState.Closed)
                    {
                        this.con.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void getConnectionString(string p_connectionString)
        {
            this.stringCon = p_connectionString;
        }

        public int ExecuteNonQuery_Command(CommandType commandType, string commandText)
        {
            openConnection();
            this.com = this.con.CreateCommand();
            this.com.CommandType = commandType;
            this.com.CommandText = commandText;
            if (checkListParamaterHasData(dbParameters))
                this.com.Parameters.AddRange(dbParameters.ToArray());
            try
            {
                return this.com.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public SqlDataReader ExecuteReader_Command(CommandType commandType, string commandText)
        {
            openConnection();
            this.com = this.con.CreateCommand();
            this.com.CommandType = commandType;
            this.com.CommandText = commandText;
            if (checkListParamaterHasData(dbParameters))
                this.com.Parameters.AddRange(dbParameters.ToArray());
            try
            {
                this.daReader = this.com.ExecuteReader();
                if (this.daReader.HasRows)
                    this.hasData = true;
                else
                    this.hasData = false;
                return this.daReader;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void AddParameters(string paramName, object objValue, SqlDbType typeSQLData, DbType typeData)
        {
            if (dbParameters == null)
            {
                dbParameters = new List<SqlParameter>();
            }
            SqlParameter inParamater = new SqlParameter();
            inParamater.ParameterName = paramName;
            inParamater.Value = objValue;
            inParamater.SqlDbType = typeSQLData;
            inParamater.DbType = typeData;
           

            dbParameters.Add(inParamater);
        }

        public void AddParameters(string paramName, object objValue, SqlDbType typeSQLData)
        {
            if (dbParameters == null)
            {
                dbParameters = new List<SqlParameter>();
            }
            SqlParameter inParamater = new SqlParameter();
            inParamater.ParameterName = paramName;
            inParamater.Value = objValue;
            inParamater.SqlDbType = typeSQLData;           


            dbParameters.Add(inParamater);
        }
        public void AddParameters(SqlParameter param)
        {
            if (dbParameters == null)
            {
                dbParameters = new List<SqlParameter>();
            }

            dbParameters.Add(param);
        }
        public void AddParameters(List<SqlParameter> param)
        {
            if (dbParameters == null)
            {
                dbParameters = new List<SqlParameter>();
            }

            dbParameters.AddRange(param);
        }
        public void AddParameters(string paramName, object objValue, SqlDbType typeData, ParameterDirection paDir)
        {
            if (dbParameters == null)
            {
                dbParameters = new List<SqlParameter>();
            }
            SqlParameter inParamater = new SqlParameter();
            inParamater.ParameterName = paramName;
            inParamater.Value = objValue;
            inParamater.SqlDbType = typeData;
            inParamater.Direction = paDir;
            dbParameters.Add(inParamater);
        }

        public void ClearParamaters()
        {
            this.dbParameters.Clear();
        }
        /// <summary>
        /// Check list SQLParamater has data in list or orther null
        /// </summary>
        /// <param name="listParamater"></param>
        /// <returns>true: Has data or not null, false</returns>
        public bool checkListParamaterHasData(List<SqlParameter> listParamater)
        {
            if (listParamater != null && listParamater.Count > 0)
                return true;
            else
                return false;
        }
        #endregion

    }
}